<template>
  <div id="app">

    <el-card shadow="always">
      <h2>规格管理</h2>

      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="specificationSearch" class="demo-form-inline">
        <el-form-item>
          <el-button type="success" plain @click="editSpecification" icon="el-icon-plus">新建</el-button>
        </el-form-item>
        <el-form-item >
          <el-button type="info" plain @click="handleRefresh" icon="el-icon-loading">刷新</el-button>
        </el-form-item>
        <div style="float: right">
          <el-form-item  >
            <el-input  v-model="specificationSearch.specName" placeholder="规格名称"></el-input>
          </el-form-item>
          <el-form-item >
            <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
          </el-form-item>
        </div>

<!--        <el-form-item >-->
<!--          <el-select v-model="studentSearch.cid" placeholder="所在班级">-->
<!--            <el-option label="所有班级" value="0"></el-option>-->
<!--            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->


      </el-form>
      <el-divider><i class="el-icon-loading"></i></el-divider>
      <el-table
          :data="specificationList"
          style="width: 100%">
        <el-table-column
            label="规格ID"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="规格名称"
            align="center"
            prop="specName">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">修改</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--新增对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="specification">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="specification.specName" autocomplete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="所在班级" :label-width="formLabelWidth" style="width: 1200px">
          <el-select v-model="student.cid" placeholder="请选择所在班级" >
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSpecification">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="specification">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="specification.specName" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSpecification">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import specificationApi from "@/api/specification"
import specificationOptionApi from "@/api/specificationOption"

export default {
  data() {
    return {
      specificationOptionList: [],
      page: 1,               // 当前页
      pageSize: 5,           // 每页大小
      total: 100,            // 总记录数
      searchSpecificationOption: {},
      specificationSearch: {},
      specificationList: [],
      dialogFormVisible: false,
      detailDialogVisible:false,
      formLabelWidth: '100px',
      specification: {},
      title: '添加规格',
    }
  },
  created() {
    // this.findSpecification()
    this.search()
  },

  methods: {
    async search() {
      const {data} = await specificationApi.search(this.page, this.pageSize, this.specificationSearch)
      this.specificationList = data.rows
      this.total = data.total
    },
    editSpecification() {
      this.dialogFormVisible = true
    },
    // 6. 保存规格
    async saveSpecification() {
      const {data: res} = await specificationApi.save(this.specification)
      if (res.code === 200) {
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      } else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleRefresh(){
      this.search()
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    }
  }
}
</script>
<style>
.el-card {
  width: 90%;
  //margin: 0 auto;
}

#app {
  display: flex;
  justify-content: center;
}

.el-pagination {
  text-align: right;
  margin-top: 15px;
}
</style>